/*
* Copyright (C) 2004-2007  Autodesk, Inc.
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of version 2.1 of the GNU Lesser
* General Public License as published by the Free Software Foundation.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
*/

#include "Pch.h"
#include "CMSTestSelect.h"

CPPUNIT_TEST_SUITE_REGISTRATION( CMSTestSelect );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CMSTestSelect, "CMSTestSelect");

CMSTestSelect::CMSTestSelect ()
{
}

CMSTestSelect::~CMSTestSelect ()
{
}

void CMSTestSelect::testBing1 ()
{
    try
    {
        FdoPtr<FdoIConnection> conn = this->GetConnection ();
        conn->SetConnectionString (L"FeatureServer=http://ecn.api.tiles.virtualearth.net/api/GetMap.ashx?;APIKey=default");
        CPPUNIT_ASSERT (FdoConnectionState_Open == conn->Open ());

        FdoPtr<FdoISelect> cmd = static_cast<FdoISelect *> (conn->CreateCommand (FdoCommandType_Select));
        cmd->SetFeatureClassName (L"roadmap");
        FdoPtr<FdoIFeatureReader> featReader = cmd->Execute();

        // Layer h(AerialWithLabels)
        CPPUNIT_ASSERT (featReader->ReadNext ());
        FdoPtr<FdoIRaster> raster = featReader->GetRaster(L"Raster");
        FdoPtr<FdoIStreamReader> stream = raster->GetStreamReader();
        CPPUNIT_ASSERT (!featReader->ReadNext ());
    }
    catch (FdoException* e)
    {
        fail(e);
    }
}

void CMSTestSelect::testYahoo1 ()
{
    try
    {
        FdoPtr<FdoIConnection> conn = this->GetConnection ();
        conn->SetConnectionString (L"FeatureServer=http://local.yahooapis.com/MapsService/V1/mapImage?;APIKey=i3M.d9_V34G5V.aulAsZT2Y7WMCiQgQEBJ6DIDhRccnkehVJLwBWbMG9TUogi9liNw--");
        CPPUNIT_ASSERT (FdoConnectionState_Open == conn->Open ());

        FdoPtr<FdoISelect> cmd = static_cast<FdoISelect *> (conn->CreateCommand (FdoCommandType_Select));
        cmd->SetFeatureClassName (L"roadmap");
        FdoPtr<FdoIFeatureReader> featReader = cmd->Execute();

        // Layer h(AerialWithLabels)
        CPPUNIT_ASSERT (featReader->ReadNext ());
        FdoPtr<FdoIRaster> raster = featReader->GetRaster (L"Raster");
        FdoPtr<FdoIStreamReader> stream = raster->GetStreamReader();
        CPPUNIT_ASSERT (!featReader->ReadNext ());
    }
    catch (FdoException* e)
    {
        fail(e);
    }
}

void CMSTestSelect::testGoogle1 ()
{
    try
    {
        FdoPtr<FdoIConnection> conn = this->GetConnection ();
        conn->SetConnectionString (L"FeatureServer=http://maps.google.com/maps/api/staticmap?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ");
        CPPUNIT_ASSERT (FdoConnectionState_Open == conn->Open ());

        FdoPtr<FdoISelect> cmd = static_cast<FdoISelect *> (conn->CreateCommand (FdoCommandType_Select));
        cmd->SetFeatureClassName (L"roadmap");
        FdoPtr<FdoIFeatureReader> featReader = cmd->Execute ();

        // Layer "roadmap"
        CPPUNIT_ASSERT (featReader->ReadNext ());
        FdoPtr<FdoIRaster> raster = featReader->GetRaster (L"Raster");
        FdoPtr<FdoIStreamReader> stream = raster->GetStreamReader();
        CPPUNIT_ASSERT (!featReader->ReadNext ());
    }
    catch (FdoException* e)
    {
        fail(e);
    }
}

// Test whole world.
void CMSTestSelect::testResampleGoogle1()
{
    FdoStringP connectStr = L"FeatureServer=http://maps.google.com/maps/api/staticmap?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ";
    FdoStringP mapType = L"roadmap";

    const FdoDouble dMinX = -180;
    const FdoDouble dMaxX = 180;
    const FdoDouble dMinY = -90;
    const FdoDouble dMaxY = 90;
    const FdoDouble dWidth = 256;
    const FdoDouble dHeight = 256;

    testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);
}

// Boundary test.
void CMSTestSelect::testResampleGoogle2 ()
{
    FdoStringP connectStr = L"FeatureServer=http://maps.google.com/maps/api/staticmap?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ";
    FdoStringP mapType = L"roadmap";

    FdoDouble dMinX = 100;
    FdoDouble dMaxX = 700;
    FdoDouble dMinY = 100;
    FdoDouble dMaxY = 500;
    FdoDouble dWidth = 600;
    FdoDouble dHeight = 800;

    testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);
}

void CMSTestSelect::testResampleGoogle3 ()
{
    FdoStringP connectStr = L"FeatureServer=http://maps.google.com/maps/api/staticmap?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ";
    FdoStringP mapType = L"roadmap";

    FdoDouble dMinX = 100;
    FdoDouble dMaxX = 150;
    FdoDouble dMinY = 10;
    FdoDouble dMaxY = 90;
    FdoDouble dWidth = 600;
    FdoDouble dHeight = 800;

    testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);
}

void CMSTestSelect::testResampleGoogle4 ()
{
    FdoStringP connectStr = L"FeatureServer=http://maps.google.com/maps/api/staticmap?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ";
    FdoStringP mapType = L"roadmap";

    FdoDouble dMinX = 12;
    FdoDouble dMaxX = 73;
    FdoDouble dMinY = 8;
    FdoDouble dMaxY = 39;
    FdoDouble dWidth = 700;
    FdoDouble dHeight = 700;

    testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);
}

// Test satellite maptype.
void CMSTestSelect::testResampleGoogle5()
{
    FdoStringP connectStr = L"FeatureServer=http://maps.google.com/maps/api/staticmap?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ";
    FdoStringP mapType = L"satellite";

    const FdoDouble dMinX = -180;
    const FdoDouble dMaxX = 180;
    const FdoDouble dMinY = -90;
    const FdoDouble dMaxY = 90;
    const FdoDouble dWidth = 256;
    const FdoDouble dHeight = 256;

    testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);
}

// Test satellite maptype.
void CMSTestSelect::testResampleGoogle6()
{
    FdoStringP connectStr = L"FeatureServer=http://maps.google.com/maps/api/staticmap?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ";
    FdoStringP mapType = L"satellite";

    const FdoDouble dMinX = -180;
    const FdoDouble dMaxX = 180;
    const FdoDouble dMinY = -90;
    const FdoDouble dMaxY = 90;
    const FdoDouble dWidth = 256;
    const FdoDouble dHeight = 256;

 //   testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);

 //   try
 //   {
	//    FdoPtr<FdoIConnection> conn = this->GetConnection ();
	//    FdoIoFileStreamP fileStream = FdoIoFileStream::Create(L"CMSOverridesGoogle.xml", L"r");
	//    conn->SetConnectionString (L"FeatureServer=http://wms.jpl.nasa.gov/wms.cgi?");
	//    conn->SetConfiguration (fileStream);
	//    CPPUNIT_ASSERT (FdoConnectionState_Open == conn->Open ());

	//    FdoPtr<FdoIDescribeSchema> cmdDescribeSchema = static_cast<FdoIDescribeSchema *> (conn->CreateCommand (FdoCommandType_DescribeSchema));
	//    FdoPtr<FdoFeatureSchemaCollection> schemas = cmdDescribeSchema->Execute ();

	//    FdoPtr<FdoISelect> cmdSelect = static_cast<FdoISelect*>(conn->CreateCommand(FdoCommandType_Select));
	//    cmdSelect->SetFeatureClassName(L"global_mosaic");

	//    FdoPtr<FdoIFeatureReader> featureReader = cmdSelect->Execute();

	//    while (featureReader->ReadNext ())
	//    {
	//	    FdoPtr<FdoIRaster> raster = featureReader->GetRaster (L"Image");
	//	    FdoPtr<FdoIStreamReader> stream = raster->GetStreamReader ();
	//	    FdoIStreamReader* streamReader = raster->GetStreamReader ();
	//	    FdoPtr<FdoIStreamReaderTmpl<FdoByte> > byteStreamReader = static_cast<FdoIStreamReaderTmpl<FdoByte>*> (streamReader);
 //   		
	//	    CPPUNIT_ASSERT (streamReader != NULL);
	//	    CPPUNIT_ASSERT (byteStreamReader != NULL);
 //   		
	//	    FdoByte buff[4096];
	//	    FdoInt64 cntTotal = 0;
	//	    FdoInt32 cntRead = 0;
	//	    do
	//	    {
	//		    cntRead = byteStreamReader->ReadNext (buff, 0 , 4096);
	//		    cntTotal += cntRead;
	//	    }
	//	    while (cntRead);
	//    }
	//}
	//catch(FdoException* ex)
	//{
 //       fail(ex);
	//}


   try
    {
        FdoPtr<FdoIConnection> conn = this->GetConnection ();
        conn->SetConnectionString (connectStr);

        // SetConfiguration
	    FdoIoFileStreamP fileStream = FdoIoFileStream::Create(L"CMSOverridesGoogle.xml", L"r");
	    conn->SetConfiguration (fileStream);

        CPPUNIT_ASSERT (FdoConnectionState_Open == conn->Open ());

        FdoICommand* cmd = conn->CreateCommand(FdoCommandType_Select);
        FdoPtr<FdoISelect> cmdSelect = static_cast<FdoISelect*>(cmd);
        cmdSelect->SetFeatureClassName(mapType);
        
        FdoPtr<FdoExpressionCollection> funcParams = FdoExpressionCollection::Create();
        FdoPtr<FdoIdentifier> rasterProp = FdoIdentifier::Create(L"Image");
        funcParams->Add(rasterProp);
        FdoPtr<FdoDataValue> minX = FdoDataValue::Create(dMinX, FdoDataType_Double);
        funcParams->Add(minX);
        FdoPtr<FdoDataValue> minY = FdoDataValue::Create(dMinY, FdoDataType_Double);
        funcParams->Add(minY);
        FdoPtr<FdoDataValue> maxX = FdoDataValue::Create(dMaxX, FdoDataType_Double);
        funcParams->Add(maxX);
        FdoPtr<FdoDataValue> maxY = FdoDataValue::Create(dMaxY, FdoDataType_Double);
        funcParams->Add(maxY);
        FdoPtr<FdoDataValue> height = FdoDataValue::Create(dHeight, FdoDataType_Double);
        funcParams->Add(height);
        FdoPtr<FdoDataValue> width = FdoDataValue::Create(dWidth, FdoDataType_Double);
        funcParams->Add(width);

        FdoPtr<FdoFunction> resampleFunc = FdoFunction::Create(L"RESAMPLE", funcParams);
        FdoPtr<FdoComputedIdentifier> resampleIdentifier = FdoComputedIdentifier::Create(L"resampledRaster", resampleFunc);
        FdoPtr<FdoIdentifierCollection> propsToSelect = cmdSelect->GetPropertyNames();
        
        // add it to the properties to select
        propsToSelect->Add(resampleIdentifier);

        FdoPtr<FdoIFeatureReader> featureReader = cmdSelect->Execute();

        while (featureReader->ReadNext ())
        {
            FdoPtr<FdoIRaster> raster = featureReader->GetRaster (L"resampledRaster");
            FdoPtr<FdoIStreamReader> stream = raster->GetStreamReader ();		
        }
    }
    catch (FdoException* e)
    {
        fail(e);
    }

}

void CMSTestSelect::testResampleBing1 ()
{
    FdoStringP connectStr = L"FeatureServer=http://ecn.api.tiles.virtualearth.net/api/GetMap.ashx?;APIKey=default";
    FdoStringP mapType = L"roadmap";

    FdoDouble dMinY = 40.714728;
    FdoDouble dMaxY = 40.714728 + 1;
    FdoDouble dMinX = -73.998672;
    FdoDouble dMaxX = -73.998672 + 1;
    FdoDouble dWidth = 1024 * 2;
    FdoDouble dHeight = 1024 * 2;

    testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);
}

void CMSTestSelect::testResampleYahoo1 ()
{
    FdoStringP connectStr = L"FeatureServer=http://local.yahooapis.com/MapsService/V1/mapImage?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ";
    FdoStringP mapType = L"roadmap";

    FdoDouble dMinY = 40.714728;
    FdoDouble dMaxY = 40.714728 + 1;
    FdoDouble dMinX = -73.998672;
    FdoDouble dMaxX = -73.998672 + 1;
    FdoDouble dWidth = 1024 * 2;
    FdoDouble dHeight = 1024 * 2;

    testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);
}

void CMSTestSelect::testResampleYahoo2 ()
{
    FdoStringP connectStr = L"FeatureServer=http://local.yahooapis.com/MapsService/V1/mapImage?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ";
    FdoStringP mapType = L"satellite";

    FdoDouble dMinX = -180;
    FdoDouble dMaxX = 180;
    FdoDouble dMinY = -90;
    FdoDouble dMaxY = 90;
    FdoDouble dWidth = 256;
    FdoDouble dHeight = 256;

    testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);
}

void CMSTestSelect::testResampleYahoo3 ()
{
    FdoStringP connectStr = L"FeatureServer=http://local.yahooapis.com/MapsService/V1/mapImage?;APIKey=ABQIAAAAa90S5XRpq6shaMXIF3zUzRTVhk_FawdprkxQ7rY9h5v2o213HxQ1T-519seO5TvavyGYMXXO_zwzOQ";
    FdoStringP mapType = L"roadmap";

    FdoDouble dMinX = -24;
    FdoDouble dMaxX = -8;
    FdoDouble dMinY = 57;
    FdoDouble dMaxY = 68;
    FdoDouble dWidth = 1024;
    FdoDouble dHeight = 1024;

    testResample(connectStr, mapType, dMinX, dMinY, dMaxX, dMaxY, dWidth, dHeight);
}

void CMSTestSelect::testResample(FdoStringP connectStr, 
                                 FdoStringP mapType,
                                const FdoDouble dMinX,
                                const FdoDouble dMinY, 
                                const FdoDouble dMaxX,
                                const FdoDouble dMaxY, 
                                const FdoDouble dWidth, 
                                const FdoDouble dHeight
                                )
{
    try
    {
        FdoPtr<FdoIConnection> conn = this->GetConnection ();
        conn->SetConnectionString (connectStr);

        CPPUNIT_ASSERT (FdoConnectionState_Open == conn->Open ());

        FdoICommand* cmd = conn->CreateCommand(FdoCommandType_Select);
        FdoPtr<FdoISelect> cmdSelect = static_cast<FdoISelect*>(cmd);
        cmdSelect->SetFeatureClassName(mapType);
        
        FdoPtr<FdoExpressionCollection> funcParams = FdoExpressionCollection::Create();
        FdoPtr<FdoIdentifier> rasterProp = FdoIdentifier::Create(L"Image");
        funcParams->Add(rasterProp);
        FdoPtr<FdoDataValue> minX = FdoDataValue::Create(dMinX, FdoDataType_Double);
        funcParams->Add(minX);
        FdoPtr<FdoDataValue> minY = FdoDataValue::Create(dMinY, FdoDataType_Double);
        funcParams->Add(minY);
        FdoPtr<FdoDataValue> maxX = FdoDataValue::Create(dMaxX, FdoDataType_Double);
        funcParams->Add(maxX);
        FdoPtr<FdoDataValue> maxY = FdoDataValue::Create(dMaxY, FdoDataType_Double);
        funcParams->Add(maxY);
        FdoPtr<FdoDataValue> height = FdoDataValue::Create(dHeight, FdoDataType_Double);
        funcParams->Add(height);
        FdoPtr<FdoDataValue> width = FdoDataValue::Create(dWidth, FdoDataType_Double);
        funcParams->Add(width);

        FdoPtr<FdoFunction> resampleFunc = FdoFunction::Create(L"RESAMPLE", funcParams);
        FdoPtr<FdoComputedIdentifier> resampleIdentifier = FdoComputedIdentifier::Create(L"resampledRaster", resampleFunc);
        FdoPtr<FdoIdentifierCollection> propsToSelect = cmdSelect->GetPropertyNames();
        
        // add it to the properties to select
        propsToSelect->Add(resampleIdentifier);

        FdoPtr<FdoIFeatureReader> featureReader = cmdSelect->Execute();

        while (featureReader->ReadNext ())
        {
            FdoPtr<FdoIRaster> raster = featureReader->GetRaster (L"resampledRaster");
            FdoPtr<FdoIStreamReader> stream = raster->GetStreamReader ();		
        }
    }
    catch (FdoException* e)
    {
        fail(e);
    }
}



